#include <coemain.h>
#include <AknViewAppUi.h>
#include <AknIconArray.h>

#include <MrLockUi.rsg>


#include "MrLock.hrh"

#include "AutolockView.h"
#include "SettingsListBox.h"
#include "EnumSettingPage.h"

#include "AppUi.h"
#include "Utils.h"
#include "ChooseApplicationDialog.h"
#include "Logger.h"

CAutolockView* CAutolockView::NewL()
{
	CAutolockView* self = new (ELeave) CAutolockView();
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
}

CAutolockView::CAutolockView()
{
}

void CAutolockView::LoadIconsL()
{
}

void CAutolockView::ConstructL()
{
	BaseConstructL(R_SETTINGS_VIEW);
	LoadSettingsL();
}

TUid CAutolockView::Id() const
{
	return TUid::Uid(EAutolockViewId);
}

void CAutolockView::LoadItemsL()
{
	iItems->Reset();
	TBuf<256> item;
	item.Append( _L("\tMr Lock\t\t") );
	if( iAutoLockEnabled == 0 )
	{//Mr. Lock is disabled
		item.Append( Utils::GetR( R_TEXT_DISABLED ) );
		iItems->AppendL( item );
	}
	else
	{
		item.Append( Utils::GetR( R_TEXT_ENABLED ) );
		iItems->AppendL( item );
		
		item.Format(_L("\t%S\t\t"), &Utils::GetR(R_TEXT_AUTOLOCK_IN));
		if( !iCustomProfiles )
		{
			item.Append(Utils::GetR(R_TEXT_ALL_PROFILES));
		}
		else
		{
			item.AppendFormat(Utils::GetR(R_TEXT_PROFILES_FORMAT), iProfileList.Count());
		}
		iItems->AppendL(item);
//
		item.Format(_L("\t%S\t\t"), &Utils::GetR(R_TEXT_AUTOLOCK_PERIOD));
		item.AppendFormat(Utils::GetR(R_TEXT_SECONDS_FORMAT), iTimeOut.Int());
		iItems->AppendL(item);
		item.Format(_L("\t%S\t\t"), &Utils::GetR(R_TEXT_LOCK_KEYPAD));
		if (iLockWhenIdle)
			item.Append(Utils::GetR(R_TEXT_LOCK_WHEN_IDLE));
		else 
			item.Append(Utils::GetR(R_TEXT_LOCK_EVERYWHERE));
		iItems->AppendL(item);
		if (EFalse == iLockWhenIdle)
		{
			item.Format(_L("\t%S\t\t"), &Utils::GetR(R_TEXT_DONT_LOCK));
			item.AppendFormat(_L("%d %S"), iAppList.Count(), &Utils::GetR(R_TEXT_PROGRAMS));
			iItems->AppendL(item);			
		}
	}
	iListBox->HandleItemAdditionL();
}

void CAutolockView::ShowSettingItemL(TInt aInd)
{
	TPtrC title = Title(aInd);
	CAknSettingPage* page = NULL;
	TBool fSettingsAccepted = EFalse;

	switch(aInd)
	{
		case 0:
		{
			iAutoLockEnabled = !iAutoLockEnabled;
			fSettingsAccepted = ETrue;
			break;
		}
		case 1:
		{
			page = (CAknSettingPage*) CEnumSettingPage::NewL(title, iCustomProfiles, iProfileList, R_PROFILE_IND_ARR, KErrNotFound);
			break;
		}
		case 2:
		{
			page = (CAknSettingPage*) CSettingsBaseView::CreateDurationSettingPageL(title, iTimeOut, KErrNotFound);
			break;
		}
		case 3:
		{
			iLockWhenIdle = !iLockWhenIdle;
			fSettingsAccepted = ETrue;
			break;	
		}
		case 4:
		{
			fSettingsAccepted = CChooseApplicationDialog::ShowDialogL( iAppList );
			break;			
		}
	}

	if(  aInd == 0 )
	{
		DefAppUi;
		if( iAutoLockEnabled )
			appUi->ShowHelpNoteL( CHelpNotes::EMrLockEnabled );
	}
	if( page )
		fSettingsAccepted = page->ExecuteLD();

	if( fSettingsAccepted )
	{//settings changed
		SaveSettingsL();
		LoadItemsL();
		DefAppUi;
		appUi->iObserver.ReloadSettings();
	}

}
